<?php


namespace App\Http\Services;


use App\Models\Member;
use App\Models\MemberBonusQueue;
use App\Models\MemberOrder;
use App\Models\MemberRecommend;
use Auth;
use DB;

class MemberServices
{
    public function getRecommendList()
    {
        return MemberRecommend::select('m1.phone as referrerPhone', 'm1.name as referrerName', 'm1.avatar as referrerAvatar', 'm2.phone as refereePhone', 'm2.name as refereeName', 'm2.avatar as refereeAvatar', 'member_recommend.created_at')
            ->leftJoin('members as m1', 'm1.id', 'member_recommend.member_id')
            ->leftJoin('members as m2', 'm2.id', 'member_recommend.recommend_id')
            ->paginate()->toArray();
    }

    public function getBalance()
    {
        $member = Auth::user();
        $balance = $member->useable_bonus_money + $member->card_money;
        $coupon = $member->card_money_coupon;
        return [
            'all' => $balance + $coupon,
            'balance' => $balance,
            'coupon' => $coupon
        ];
    }

    public function getNum()
    {
        return [
            'lately_order' => MemberOrder::where('member_id', Auth::id())->sum('money'),
            'share' => Member::where('id', Auth::id())->value('useable_share_money')
        ];
    }

    public function getLatelyOrder()
    {
        return MemberOrder::select([
            'member_order.money',
            'member_order.pay_time',
            'store.logo',
            'store.name'
        ])
            ->leftJoin('store', 'store.id', '=', 'member_order.store_id')
            ->where('member_order.member_id', Auth::id())
            ->where('member_order.is_pay', 1)
            ->get();
    }

    public function bonusRank()
    {
        $member_bonus_num = MemberBonusQueue::where("status", 0)->count();
        $sub = DB::table(DB::raw("member_bonus_queue a,(select @rownum :=0) b"))
            ->select(DB::raw("a.id, a.member_id,(@rownum:=@rownum+1) as rownum"))
            ->where("a.status", 0)->orderBy("created_at", "asc");
        $rowno = DB::table(DB::raw("({$sub->toSql()}) as c"))
            ->mergeBindings($sub)
            ->where('c.member_id', \Auth::id() ? \Auth::id() :6)
            ->value('c.rownum');
        return [
            'bonus_rank_num' => $member_bonus_num,
            'rank' => $rowno?:0
        ];
    }

    public function award()
    {
        return [
            'award_all' => MemberBonusQueue::where('member_id', Auth::id())->where('status', 1)->sum('money'),
            'share' => Member::where('id', Auth::id())->value('useable_share_money')
        ];
    }

    public function rankList()
    {

    }

    public function memberRecommendRebateData()
    {
        return DB::table('member_card_income_log as mi')
            ->select('mi.id','m.name','m.phone', 'm.avatar', 'mi.created_at', 'mi.money')
            ->leftJoin('members as m', 'm.id', '=', 'mi.member_id')
            ->paginate()->toArray();
    }

    public function getMemberRecommendStore()
    {
        DB::table('member_recommend_store as mrs')
            ->select('mrs.id','mrs.created_at', 'm.name','m.avatar', 'm.phone', 's.name as s_name')
            ->leftJoin('members as m', 'm.id', '=', 'mrs.member_id')
            ->leftJoin('store as s', 's.id', '=', 'mrs.store_id')
            ->paginate()
            ->toArray();
    }

}